#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;
struct array
{
    int size;
    int used;
    int *arr;
};

bool Init(struct array *arr)
{
    arr->arr = (int *)malloc(arr->size * sizeof(int));
    return (arr->arr) ? true : false;
}

//增加
int Insert(struct array *arr, int elem)
{
    int pos = 0;
    if (arr->size <= arr->used)
        return -1;

    for (; pos < arr->used; pos++)
    {
        if (arr->arr[pos] > elem)
            break;
    }

    if (pos < arr->used)
    {
        memmove(&arr->arr[pos + 1], &arr->arr[pos],
                (arr->used - pos) * sizeof(int));
    }

    arr->arr[pos] = elem;
    arr->used++;
    return pos;
}

//删除
int Delete(struct array *arr, int pos)
{
    if (pos < 0 || pos >= arr->used)
        return -1;

    memmove(&arr->arr[pos], &arr->arr[pos + 1],
            (arr->used - pos - 1) * sizeof(int));
    arr->used--;
    return 0;
}

//修改
void Modify(struct array *arr, int elem)
{
}

//查找
int Search(struct array *arr, int elem)
{
    int pos = -1;
    for (int i = 0; i < arr->used; i++)
    {
        if (arr->arr[i] == elem)
        {
            pos = i + 1;
            break;
        }
    }
    return pos;
}

//输出
void Output(struct array *arr)
{
    for (int pos = 0; pos < arr->used; pos++)
    {
        cout << arr->arr[pos] << endl;
    }
}

int main()
{
    int idx;
    struct array test = {10, 0, NULL};
    if (!Init(&test))
        return 0;
    Insert(&test, 4);
    Insert(&test, 1);
    Insert(&test, 3);
    Output(&test);
    cout << "search:" << Search(&test, 4) << endl;
    Delete(&test, 0);
    Output(&test);
    return 0;
}